<template>
<div class="app-view-container">
  <mu-appbar class="app-popup-topbar">
    <mu-raised-button class="size-s" label="返回" slot="left" @click="$router.back()"/>
  </mu-appbar>
  <div class="app-popup-content" ref="scrollContainer">
    <mu-refresh-control :refreshing="refreshing" :trigger="trigger" @refresh="getData"/>
    <mu-list class="app-list" v-for="fileObjct in list">
      <mu-sub-header><mu-icon value="library_books"/>{{fileObjct.name}}</mu-sub-header>
      <app-file-list :list="fileObjct.list.map(function(x){return {id:x.textFileId,name:x.baseFileName||'',thumb:x.pattern||'',src:x.bigPattern||''} })" @click="handleClick"></app-file-list>
    </mu-list>
    <mu-raised-button v-if="!loading&&!refreshing&&currentPage<totalPage" fullWidth label="加载更多" @click="loadMore"/>
    <mu-infinite-scroll v-show="!refreshing" :scroller="scroller" :loading="loading" @load="loadMore"/>
  </div>
</div>
</template>

<script>
import services from 'services'
  export default {
    data:function() {
      return {
        list:[],
        currentPage:0,
        totalPage:1,
        loading: false,
        scroller: null,
        refreshing: false,
        trigger: null,
      }
    },
    computed:{
    },
    mounted () {
      this.scroller = this.$refs.scrollContainer;
      this.trigger = this.$refs.scrollContainer;
      this.getData ()
    },
    beforeRouteUpdate (to,form,next){
      this.getData();
      next()
    },
    methods: {
      getData (){
        this.refreshing = true
        this.currentPage=0;
        this.list=[];
        this.loadMore ()
      },
      loadMore () {
        var vm=this;
        if (vm.currentPage>=vm.totalPage&&vm.totalPage!==0||vm.loading) return
        vm.loading = true
        //获取全文搜索列表
        services.ArchiveService.getFilesGroup({
          data:Object.assign({
            page:vm.currentPage+1,
            pageSize:10,
          },vm.$route.query),
          success:(data)=>{
            vm.currentPage==0&&(vm.list=[])
            var list=[];
            for(var name in data.aaData){
              list.push({
                id:name.substring(0,name.indexOf(".")),
                name:name.substring(name.indexOf(".")+1,name.length),
                list:data.aaData[name]
              })
            }
            vm.list=vm.list.concat(list);
            vm.totalPage=data.totalPage;
            vm.currentPage++
          },
          complete:()=>{
            vm.loading = false
            vm.refreshing = false
          }
        })
      },
      handleClick (itemData){
        if (!itemData.src||!itemData.thumb) {
          //下载非图片文件
          alert('download：'+itemData.id)
        }
      }
    }
  }
</script>

<style>
</style>
